<?php
declare (strict_types=1);

namespace CZTools\Hyperf\base;

use CZToolsHpf\Base\AuthUserAction;
use CZToolsHpf\Base\Config;
use cunzan\traits\ReturnContent;
use think\exception\ValidateException;
use think\facade\Db;
use think\Validate;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    use ReturnContent;

    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     */
    public function __construct(int $moduleCode = 0)
    {

    }

    // 初始化
    protected function initialize()
    {

    }

    /**
     * 验证数据
     * @access protected
     * @param array $data 数据
     * @param string|array $validate 验证器名或者验证规则数组
     * @param array $message 提示信息
     * @param bool $batch 是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }

    //配置数据字典
    public function config()
    {
        $list = Db::table('system_config')
            ->field('id,p_code,code,name,type,value,value2,value3,value4,img,remark')
            ->order('type asc ,sort asc,id asc')
            ->select();
        $newlist=array();
        foreach ($list as $key=>$value){
            if(!isset($newlist[$value['type']])){
                $newlist[$value['type']]=array();
            }

            array_push($newlist[$value['type']],$value);
        }
        return json(['code' => 1, 'msg' => '请求成功!', 'system' => $newlist]);
    }

    protected static function getAreaCodePreByLevel(string $level, string $areaCode)
    {
        $prefix = "";
        if($level && $areaCode){
            switch ($level){
                case 0:
                    $prefix = substr($areaCode, 0, 2);
                    break;
                case 1:
                    $prefix = substr($areaCode, 0, 4);
                    break;
                case 2:
                    $prefix = substr($areaCode, 0, 6);
                    break;
                case 3:
                    $prefix = substr($areaCode, 0, 9);
                    break;
                case 4:
                    $prefix = $areaCode;
                    break;
                default:
                    return '';
            }
        }
        return $prefix;
    }

    public static function getFullAreaCodeByLevel(int $level, string $areaCode)
    {
        $fullAreaCode = "";
        if ($level && $areaCode) {
            switch ($level) {
                case 0: //省
                    $fullAreaCode = str_pad(substr($areaCode, 0, 2), 12, "0");
                    break;
                case 1: //市
                    $fullAreaCode = str_pad(substr($areaCode, 0, 4), 12, "0");
                    break;
                case 2: //区
                    $fullAreaCode = str_pad(substr($areaCode, 0, 6), 12, "0");
                    break;
                case 3: //镇
                    $fullAreaCode = str_pad(substr($areaCode, 0, 9), 12, "0");
                    break;
                case 4: //村
                    $fullAreaCode = $areaCode;
                    break;
                default:
                    return '';
            }
        }
        return $fullAreaCode;
    }

}
